<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch" label-width="68px">
      <el-row>
        <el-col :span="4">
          <el-form-item label="设备名称" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入设备名称" clearable @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="设备编号" prop="code">
            <el-input v-model="queryParams.code" placeholder="请输入设备编号" clearable @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="序列号" prop="sn">
            <el-input v-model="queryParams.sn" placeholder="请输入序列号" clearable @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="MAC" prop="mac">
            <el-input v-model="queryParams.mac" placeholder="请输入MAC" clearable @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="设备分类" prop="categoryId">
            <selectDeviceCategory v-model="queryParams.categoryId"></selectDeviceCategory>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="使用部门" prop="deptId">
            <selectDept v-model="queryParams.deptId"></selectDept>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4">
          <el-form-item label="供应商" prop="supplierId">
            <selectSupplier v-model="queryParams.supplierId"></selectSupplier>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="品牌" prop="brandId">
            <select-brand v-model="queryParams.brandId"></select-brand>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="工作中心" prop="centerId">
            <selectCenter v-model="queryParams.centerId"></selectCenter>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="设备状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择设备状态" clearable size="small">
              <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DEVICE_STATUS)"
                           :key="dict.value" :label="dict.label" :value="dict.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="使用状态" prop="useStatus">
            <el-select v-model="queryParams.useStatus" placeholder="请选择使用状态" clearable size="small">
              <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DEVICE_USE_STATUS)"
                           :key="dict.value" :label="dict.label" :value="dict.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="工厂" prop="factoryId">
            <selectFactory v-model="queryParams.factoryId"></selectFactory>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          </el-form-item>

        </el-col>
      </el-row>
    </el-form>
    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['device:device:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['device:device:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" align="center" prop="id" fixed />
      <el-table-column label="设备名称" align="center" prop="name" fixed />
      <el-table-column label="设备编号" align="center" prop="code" fixed />
      <el-table-column label="规格" align="center" prop="spec" />
      <el-table-column label="序列号" align="center" prop="sn" />
      <el-table-column label="MAC" align="center" prop="mac" />
      <el-table-column label="设备分类" align="center" prop="categoryName" />
      <el-table-column label="供应商" align="center" prop="supplierName" />
      <el-table-column label="品牌" align="center" prop="brandName" />
      <el-table-column label="单位" align="center" prop="unitName" />
      <el-table-column label="使用人员" align="center" prop="userName" />
      <el-table-column label="使用部门" align="center" prop="deptName" />
      <el-table-column label="工厂" align="center" prop="factoryName" />
      <el-table-column label="工作中心" align="center" prop="centerName" />
      <el-table-column label="管理人员" align="center" prop="adminName" />
      <el-table-column label="设备状态" align="center" prop="status">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.DEVICE_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="使用状态" align="center" prop="useStatus">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.DEVICE_USE_STATUS" :value="scope.row.useStatus" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['device:device:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDetail(scope.row)"
                     v-hasPermi="['device:device:update']">明细</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['device:device:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="1400px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="设备名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入设备名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备编号" prop="code">
              <el-input v-model="form.code" placeholder="请输入设备编号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="规格" prop="spec">
              <el-input v-model="form.spec" placeholder="请输入规格" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="序列号" prop="sn">
              <el-input v-model="form.sn" placeholder="请输入序列号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="MAC" prop="mac">
              <el-input v-model="form.mac" placeholder="请输入MAC" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备分类" prop="categoryId">
              <selectDeviceCategory v-model="form.categoryId"></selectDeviceCategory>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="供应商" prop="supplierId">
              <selectSupplier v-model="form.supplierId"></selectSupplier>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="品牌" prop="brandId">
              <select-brand v-model="form.brandId"></select-brand>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单位" prop="unitId">
              <selectUnit v-model="form.unitId"></selectUnit>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="采购日期" prop="procureTime">
              <el-date-picker clearable v-model="form.procureTime" type="date" value-format="timestamp" placeholder="选择采购日期" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="管理人员" prop="admin">
              <selectSingleUser  v-model="form.adminId" :isCheck="true"></selectSingleUser>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="用途说明" prop="illustrate">
          <el-input v-model="form.illustrate" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="图片">
          <imageUpload v-model="form.images"/>
        </el-form-item>
        <el-form-item label="文件">
          <fileUpload v-model="form.files"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--明细-->
    <el-dialog :title="detail.title" :visible.sync="detail.open" width="1400px"
                @close="closeDetail" v-if="detail.open"
               :destroy-on-close="true"  v-dialogDrag append-to-body>
      <el-tabs v-model="detail.activeName" type="card" @tab-click="handleClick">
        <el-tab-pane label="报修申请" name="first">
          <device-detail-fault :device-id-tmp="detail.deviceIdTmp"></device-detail-fault>
        </el-tab-pane>
        <el-tab-pane label="领用申请" name="second">
          <device-detail-receive :device-id-tmp="detail.deviceIdTmp"></device-detail-receive>
        </el-tab-pane>
        <el-tab-pane label="退还申请" name="third">
          <device-detail-refund :device-id-tmp="detail.deviceIdTmp"></device-detail-refund>
        </el-tab-pane>
        <el-tab-pane label="设备履历" name="fourth">
          <device-detail-resume :device-id-tmp="detail.deviceIdTmp"></device-detail-resume>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
  </div>
</template>

<script>
import { createDevice, updateDevice, deleteDevice, getDevice, getDevicePage, exportDeviceExcel } from "@/api/device/device";
import ImageUpload from '@/components/ImageUpload';
import FileUpload from '@/components/FileUpload';
import SelectBrand from '@/components/SelectBrand';
import SelectCenter from "@/components/SelectCenter"
import SelectFactory from "@/components/SelectFactory"
import SelectDept from "@/components/SelectDept"
import SelectSupplier from "@/components/SelectSupplier"
import SelectSingleUser from "@/components/SelectSingleUser"
import SelectUnit from "@/components/SelectUnit"
import SelectDeviceCategory from "@/components/SelectDeviceCategory"
import DeviceDetailFault from "@/components/DeviceDetailFault/index.vue";
import DeviceDetailReceive from "@/components/DeviceDetailReceive/index.vue";
import DeviceDetailRefund from "@/components/DeviceDetailRefund/index.vue";
import DeviceDetailResume from "@/components/DeviceDetailResume/index.vue";

export default {
  name: "",
  components: {
    ImageUpload,
    FileUpload,
    SelectBrand,
    SelectCenter,
    SelectFactory,
    SelectDept,
    SelectSupplier,
    SelectSingleUser,
    SelectUnit,
    SelectDeviceCategory,
    DeviceDetailFault,
    DeviceDetailReceive,
    DeviceDetailRefund,
    DeviceDetailResume
  },
  data() {
    return {
      detail:{
        title: '明细',
        open: false,
        activeName: 'first',
        deviceIdTmp: null
      },
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备基本信息列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        name: null,
        code: null,
        sn: null,
        mac: null,
        categoryId: null,
        supplierId: null,
        brandId: null,
        deptId: null,
        centerId: null,
        factoryId: null,
        status: null,
        useStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: "设备名称不能为空", trigger: "blur" }],
        code: [{ required: true, message: "设备编号不能为空", trigger: "blur" }],
        categoryId: [{ required: true, message: "设备分类不能为空", trigger: "change" }],
        supplierId: [{ required: true, message: "供应商不能为空", trigger: "change" }],
        brandId: [{ required: true, message: "品牌不能为空", trigger: "change" }],
        unitId: [{ required: true, message: "单位不能为空", trigger: "change" }],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 关闭明细对话框回调
    closeDetail(){
      this.detail.activeName = 'first'
      this.detail.open = false
      this.detail.deviceIdTmp = null
    },
    // 点击明细页签回调
    handleClick(tab, event) {
      console.log(tab, event);
    },
    /** 查看明细*/
    handleDetail(row){
      this.detail.activeName = 'first'
      this.detail.open = true
      this.detail.deviceIdTmp = row.id
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getDevicePage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        code: undefined,
        spec: undefined,
        sn: undefined,
        mac: undefined,
        illustrate: undefined,
        remark: undefined,
        categoryId: undefined,
        supplierId: undefined,
        brandId: undefined,
        unitId: undefined,
        deptId: undefined,
        centerId: undefined,
        factoryId: undefined,
        adminId: undefined,
        userId: undefined,
        loacation: undefined,
        status: undefined,
        useStatus: undefined,
        procureTime: undefined,
        images: undefined,
        files: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备基本信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getDevice(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备基本信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateDevice(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createDevice(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除设备基本信息编号为"' + id + '"的数据项?').then(function() {
          return deleteDevice(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有设备基本信息数据项?').then(() => {
          this.exportLoading = true;
          return exportDeviceExcel(params);
        }).then(response => {
          this.$download.excel(response, '设备基本信息.xls');
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
